package org.dromara.work.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.work.domain.SgjBeian;
import org.dromara.work.domain.SgjBeianReply;
import org.dromara.work.domain.bo.SgjBeianReplySaveBo;
import org.dromara.work.domain.bo.SgjBeianSaveBo;
import org.dromara.work.domain.vo.SgjBeianVo;
import org.dromara.work.domain.vo.SgjGeoVo;
import org.dromara.work.mapper.SgjBeianMapper;
import org.dromara.work.mapper.SgjBeianReplyMapper;
import org.dromara.work.service.ISgjBeianService;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;

/**
 * 测试单表Service业务层处理
 *
 * @author Lion Li
 * @date 2021-07-26
 */
@RequiredArgsConstructor
@Service
public class ISgjBeianServiceImpl implements ISgjBeianService {

    private final SgjBeianMapper baseMapper;

    private final SgjBeianReplyMapper replyMapper;

    @Override
    public SgjBeianVo queryById(String id) {
        SgjBeian base= baseMapper.selectOne(new LambdaQueryWrapper<SgjBeian>().eq(SgjBeian::getYwid,id));
        SgjBeianReply reply= replyMapper.selectOne(new LambdaQueryWrapper<SgjBeianReply>().eq(SgjBeianReply::getYwid,id));
        SgjGeoVo geo=baseMapper.queryGeoFile(id);
        SgjBeianVo entity=new SgjBeianVo();
        if(base!=null){
            entity.setBase(base);
        }
        if(reply!=null){
            entity.setApprove(reply);
        }
        if(geo!=null){
            entity.setGeo(geo);
        }
        return entity;
    }


    @Override
    public Boolean insertByBo(SgjBeianSaveBo bo) {
        SgjBeian add = MapstructUtils.convert(bo, SgjBeian.class);
        validEntityBeforeSave(add);
        add.setShape("'"+bo.getShape()+"'");
        boolean flag = baseMapper.insertGeoPolygonEntity(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertReplyByBo(SgjBeianReplySaveBo bo) {
        SgjBeianReply add = MapstructUtils.convert(bo, SgjBeianReply.class);
        //validEntityBeforeSave(bo);
        boolean flag = replyMapper.insert(add) > 0;
        if (flag) {
            if (add != null) {
                bo.setId(add.getId());
            }
        }
        // 目标URL
        String url = "http://172.27.247.36:8075/addrSearch/source/api/plot/applyRecordOut";
        // 创建JSON对象
        JSONObject jsonBody = new JSONObject();
        jsonBody.putOnce("ywid", bo.getYwid());
        jsonBody.putOnce("state", bo.getState());
        jsonBody.putOnce("record_name",bo.getRecordName());
        jsonBody.putOnce("approval_msg",bo.getApprovalMsg());
        jsonBody.putOnce("approval",bo.getApproval());

        // 获取token
        String time= Calendar.getInstance().getTimeInMillis()+"";
        String md5= SecureUtil.md5(time+"123@Abc.com");
        String json= HttpUtil.get("http://172.27.247.36:8075/addrSearch/addrMatch/auth/getToken?user=ezsmzj&secret="+md5+"&time="+time);
        JSONObject obj= JSONUtil.parseObj(json);
        String token="";
        if(obj.getJSONObject("data")!=null){
             token=obj.getJSONObject("data").getStr("token");
        }

        // 发送POST请求
        String response = HttpUtil.createPost(url)
            .header("token", token)
            .body(jsonBody.toString())
            .execute()
            .body();

        // 打印响应结果
        System.out.println(response);
        return flag;
    }

    @Override
    public Boolean updateByBo(SgjBeianSaveBo bo) {
        SgjBeian update = MapstructUtils.convert(bo, SgjBeian.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SgjBeian entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 做一些业务上的校验,判断是否需要校验
            List<SgjBeian> list = baseMapper.selectByIds(ids);
            if (list.size() != ids.size()) {
                throw new ServiceException("您没有删除权限!");
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public Boolean saveBatch(List<SgjBeian> list) {
        return baseMapper.insertBatch(list);
    }
}
